const path = require("path");

const Koa = require("koa");
const { koaBody } = require("koa-body");
const koaJsonError = require("koa-json-error");
const router = require("../router");

const app = new Koa();
const kje = koaJsonError({
  format: (err) => ({
    code: err.status || 500,
    message: err.message,
    result: err.stack,
  }),
});

app
  .use(kje)
  .use(
    koaBody({
      multipart: true,
      formidable: {
        uploadDir: path.join(__dirname, "../static"),
        keepExtensions: true,
        onFileBegin(name, file) {},
      },
    })
  )
  .use(router.routes())
  .use(router.allowedMethods());
module.exports = app;
